import {Controller, Get, Post, Body, Patch, Param, Delete, Query} from '@nestjs/common';
import { BlogsService } from './blogs.service';
import { SaveBlogDto } from './dto/save-blog.dto';
import { UpdateBlogDto } from './dto/update-blog.dto';
import {ApiOperation, ApiTags} from "@nestjs/swagger";
import {ApiResult} from "../../common/decorators/api-result.decorator";
import {BlogEntity} from "./entities/blog.entity";
import {ResultData} from "../../common/utils/result";
import {BlogTagsEntity} from "./entities/blogTags.entity";
import {UpdateBlogTagsDto} from "./dto/save-blog-tags.dto";
import {QueryBlogDto} from "./dto/query-blog.dto";
import {QueryTagsDto} from "./dto/query-tags.dto";
import {AllowAnon} from "../../common/decorators/allow-anon.decorator";

@ApiTags('文章')
@Controller('blogs')
export class BlogsController {
  constructor(private readonly blogsService: BlogsService) {}

  @Post('saveBlog')
  @ApiOperation({ summary: '新增文章' })
  @ApiResult(ResultData)
  saveBlogs(@Body() saveBlogDto: SaveBlogDto) {
    if ("id" in saveBlogDto) {
      if (!!saveBlogDto.id) {
        return this.blogsService.updateBlog(saveBlogDto as UpdateBlogDto);
      }
    }
    return this.blogsService.createBlog(saveBlogDto);
  }
  @AllowAnon()
  @Get('getBlogs')
  @ApiOperation({ summary: '获取文章列表' })
  @ApiResult(BlogEntity)
  getBlogs(@Query() queryBlogDto: QueryBlogDto) {
    return this.blogsService.getBlogs(queryBlogDto);
  }

  @AllowAnon()
  @Get('getBlogContent')
  @ApiOperation({ summary: '获取文章内容' })
  @ApiResult(BlogEntity)
  getBlogContent(@Query() queryParams:{id:number}) {
    return this.blogsService.getBlogContent(queryParams.id);
  }

  @Delete('deleteBlog')
  @ApiOperation({ summary: '删除文章内容' })
  @ApiResult(ResultData)
  deleteBlogItem(@Query() deleteBlogDto: {id:number}) {
    if(deleteBlogDto.id){
      return this.blogsService.deleteBlogItem(Number(deleteBlogDto.id))
    }
    return ResultData.fail(500,'请传入id',null)
  }

  @AllowAnon()
  @Get('getTags')
  @ApiOperation({ summary: '获取文章标签' })
  @ApiResult(BlogTagsEntity)
  getBlogTags(@Query() queryTagsDto: QueryTagsDto) {
    return this.blogsService.getBlogTags(queryTagsDto);
  }


  @Post('saveTag')
  @ApiOperation({ summary: '新增/修改文章标签' })
  @ApiResult(ResultData)
  saveBlogTags(@Body() updateBlogTagsDto: UpdateBlogTagsDto) {
    if(updateBlogTagsDto.id){
      return this.blogsService.updateBlogTag(updateBlogTagsDto);
    }
    return this.blogsService.createBlogTag(updateBlogTagsDto);
  }

  @Delete('deleteTag')
  @ApiOperation({ summary: '删除文章标签' })
  @ApiResult(ResultData)
  deleteBlogTag(@Query() deleteBlogTagDto: {id:number}) {
    if(deleteBlogTagDto.id){
      return this.blogsService.deleteBlogTag(Number(deleteBlogTagDto.id))
    }
    return ResultData.fail(500,'请传入id',null)
  }
}
